﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;

namespace RimZombieGirl
{
    public class GameCondition_BloodMoon : GameCondition//TODO：添加红月期间邪祟袭击
    {
        private static readonly SkyColorSet ScarletHazeColors =
            new SkyColorSet(new ColorInt(255, 60, 60).ToColor,   // 主天色：深红
                            new ColorInt(180, 30, 30).ToColor,   // 辅助光：暗红
                            new ColorInt(255, 100, 100).ToColor, // 环境光：绯红
                            0.45f);

        private List<SkyOverlay> overlays = new List<SkyOverlay>
        {
            new WeatherOverlay_Fog() // 可以自定义 overlay，或者直接 Fog 效果
        };

        public override int TransitionTicks => 5000;

        public override void GameConditionTick()
        {
            base.GameConditionTick();

            foreach (Map map in AffectedMaps)
            {
                if (HiddenByOtherCondition(map)) continue;

                // 遍历地图上的所有 Pawn
                foreach (Pawn pawn in map.mapPawns.AllPawnsSpawned)
                {
                    if (!pawn.RaceProps.IsFlesh || pawn.Dead) continue;
                    if (!pawn.Position.Roofed(map)) // 只对室外生效
                    {
                        // 判断是否为邪祟
                        if (pawn.kindDef.defName == "Pig")
                        {
                            HediffDef rageHediff = HediffDef.Named("RI_BerserkBuff");
                            if (pawn.health.hediffSet.GetFirstHediffOfDef(rageHediff) == null)
                            {
                                pawn.health.AddHediff(rageHediff);
                            }
                        }
                    }
                }
            }
        }

        public override SkyTarget? SkyTarget(Map map)
        {
            return new SkyTarget(0.45f, ScarletHazeColors, 1f, 1f);
        }

        public override float SkyTargetLerpFactor(Map map)
        {
            return GameConditionUtility.LerpInOutValue(this, TransitionTicks, 0.5f);
        }

        public override List<SkyOverlay> SkyOverlays(Map map) => overlays;

        public override void End()
        {
            base.End();
            // 结束时清除所有邪祟的狂暴状态
            foreach (Map map in AffectedMaps)
            {
                foreach (Pawn pawn in map.mapPawns.AllPawnsSpawned)
                {
                    if (!pawn.RaceProps.IsFlesh || pawn.Dead) continue;
                    if (pawn.kindDef.defName == "Pig")
                    {
                        HediffDef rageHediff = HediffDef.Named("RI_BerserkBuff");
                        var hediff = pawn.health.hediffSet.GetFirstHediffOfDef(rageHediff);
                        if (hediff != null)
                        {
                            pawn.health.RemoveHediff(hediff);
                        }
                    }
                }
            }
        }
    }
}
